<?php
include 'HtmlParserModel.php';
$opts = array(
    'http' => array(
        'method' => "GET",
        'timeout' => 60,
    )
);
$sex8Host = 'http://avbaidu.net/';
$context = stream_context_create($opts);

set_time_limit(0);
$dsn = "mysql:host=localhost;dbname=bhdw";
$username = 'root';
$password = '123456';
$table = 'sex8';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8';");
$dbConnection = new PDO($dsn, $username, $password, $options);
$stmt = $dbConnection->prepare('INSERT INTO ' . $table . ' (pic_from,pic_id,pic_tag,pic_category,pic_album,pic_detail_url) VALUES (:pic_from,:pic_id,:pic_tag,:pic_category,:pic_album,:pic_detail_url)');
$stmt2 = $dbConnection->prepare('SELECT COUNT(*) as count FROM ' . $table . ' WHERE pic_id=:pic_id AND pic_detail_url=:pic_detail_url');
$stmt3 = $dbConnection->query('SELECT * FROM sex8_html_url WHERE is_done <> 1');
$stmt4 = $dbConnection->prepare('UPDATE sex8_html_url SET is_done=1 WHERE id=:id');
$t1 = time();
while ($row = $stmt3->fetch(PDO::FETCH_ASSOC)) {
    getImageURL($row);
    echo "\n";
}
echo '[耗时信息]' . (time() - $t1) . '秒' . "\n";

//获取页面中的贴图
function getImageURL($data) {
    global $sex8Host, $stmt, $stmt2, $stmt4;

    $webData = get_web_data($sex8Host . $data['url']);
    if ($webData) {
        echo '[获取成功]' . date('Y-m-d H:i:s|') . $data['url'] . "\n";
        $html = new HtmlParserModel($webData);
        try {
            if (!method_exists($html, 'find') || !method_exists($html->find('#read_tpc', 0), 'find')) {
                throw new Exception('[解析失败]' . date('Y-m-d H:i:s|') . $data['url']);
            }
            $title = $html->find('#subject_tpc', 0)->getPlainText();
            $data['category'] = strtr($data['category'], array('[' => '', ']' => ''));
            $title = preg_replace('/【[^【】]*】/', '', $title);
            foreach ($html->find('#read_tpc', 0)->find('img') as $image) {
                $imageURL = $image->attribute['src'];
                $data_new = array(':pic_from' => 'sex8', ':pic_id' => $data['id'], ':pic_tag' => $data['tag'], ':pic_category' => $data['category'], ':pic_album' => $title, ':pic_detail_url' => $imageURL);
                $stmt2->execute(array(':pic_id' => $data['id'], ':pic_detail_url' => $data_new[':pic_detail_url']));
                $count = $stmt2->fetch(PDO::FETCH_ASSOC);
                if ($count['count']) {
                    echo '[插入失败]' . date('Y-m-d H:i:s|') . $data_new[':pic_detail_url'] . '已存在' . "\n";
                } else {
                    $stmt->execute($data_new);
                    echo '[插入成功]' . date('Y-m-d H:i:s|') . $data_new[':pic_detail_url'] . "\n";
                }
                $data_new = $imageURL = null;
            }
            $stmt4->execute(array(':id' => $data['id']));
        } catch (Exception $ex) {
            echo $ex->getMessage() . "\n";
            file_put_contents('/home/xukun/log/sex8_error.txt', date('[Y-m-d H:i:s]') . $ex->getMessage() . "\n", FILE_APPEND);
        }

        $html = NULL;
    } else {
        echo '[获取失败]' . date('Y-m-d H:i:s|') . $data['url'] . "\n";
    }
}

//获取网页
function get_web_data($url) {
    global $context;
    for ($i = 0; $i < 3; $i++) {
        @$web_data = file_get_contents($url, FALSE, $context);
        if ($web_data) {
            return $web_data;
        }
    }
    return FALSE;
}

